﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Data;
using System.Data.SqlClient;

using Repositories;
using Domain;

namespace Services
{
	/// <summary>
	/// 
	/// </summary>
	public class PostingService
	{
		public PostingService()
		{
		}

		/// <summary>
		/// Get all posts of a topic of a specific page
		/// </summary>
		/// <param name="topicID"></param>
		/// <param name="page"></param>
		/// <returns></returns>
		public IList<Post> getPostings(Int64 topicID, Int16 page, Int16 pageSize, string userID, ref string currentUserRole)
		{
			PostingRepository postingRep = new PostingRepository();
			return postingRep.getPostings(topicID, page, pageSize, userID, ref currentUserRole);
		}


		/// <summary>
		/// Insert a post into the database. The can be 'a new post' or 'a reply'
		/// </summary>
		/// <param name="message"></param>
		/// <param name="topicID"></param>
		/// <param name="userID"></param>
		public void InsertPosting(string message, Int64 topicID, string username, string userID, string ipaddress, out string postID, out int postCount)
		{
			string parsedMessage = ParseMessage(message);

			PostingRepository postingRep = new PostingRepository();
			postingRep.InsertPosting(parsedMessage, topicID, username, userID, ipaddress, out postID, out postCount);
		}


		/// <summary>
		/// Insert a post into the database. The can be 'a new post' or 'a reply'
		/// </summary>
		/// <param name="message"></param>
		/// <param name="topicID"></param>
		/// <param name="userID"></param>
		public void InsertPosting(string message, Int64 topicID, string username, string userID, string ipaddress, SqlConnection connection, SqlTransaction transaction)
		{
			string parsedMessage = ParseMessage(message);

			PostingRepository postingRep = new PostingRepository();
			postingRep.InsertPosting(parsedMessage, topicID, username, userID, ipaddress, connection, transaction);
		}




		/// <summary>
		/// Insert a post into the database. The can be 'a new post' or 'a reply'
		/// </summary>
		/// <param name="message"></param>
		/// <param name="topicID"></param>
		/// <param name="userID"></param>
		public void UpdatePosting(string message, Int64 postID)
		{
			string parsedMessage = ParseMessage(message);

			PostingRepository postingRep = new PostingRepository();
			postingRep.UpdatePosting(parsedMessage, postID);
		}








		/// <summary>
		/// Read a specific post from the database
		/// </summary>
		/// <param name="postID"></param>
		/// <returns>Dataset with the content of the posting</returns>
		public DataSet getPost(Int64 postID)
		{
			PostingRepository postingRep = new PostingRepository();
			return postingRep.getPost(postID);
		}



		

		/// <summary>
		/// Delete a specific post from the database
		/// </summary>
		/// <param name="postID"></param>
		/// <returns>Dataset with the content of the posting</returns>
		public void DeletePost(Int64 postID, Int64 topicID)
		{
			PostingRepository postingRep = new PostingRepository();
			postingRep.DeletePost(postID, topicID);
		}
		 


		/// <summary>
		/// Parse a message
		/// All html-tags are removed, and all BB-tags like '[b]...[/b]' are converted to html-style tags like '<b>...</b>'
		/// </summary>
		/// <returns>string with the parsed message, with html-style tags</returns>
		public string ParseMessage(string message)
		{
			//string parsedMessage = RemoveHTML(message);
			//parsedMessage = ReplaceBBTags(parsedMessage);

			string parsedMessage = ReplaceBBTags(message);

			return parsedMessage;
		}


		/// <summary>
		/// Parse a message, so all bulletin board tags like [b]...[/b] are converted to html-style tags like <b>...</b>
		/// </summary>
		/// <returns>string with the parsed message, with html-style tags</returns>
		protected string ReplaceBBTags(string message)
		{
			//All parsed tags are case insensitive. So [b]...[/b] is parsed, as well as [B]...[/B]

			//The following tags are parsed:
			//	bold			[b]...[/b]

			//The following tags are not yet parsed, but have to be parsed:
			//	italic			[i]...[/i]
			//	underline		[u]...[/u]
			//	strikethrough	[s]...[/s]
			//	hyperlink		[url=...]...[/url]
			//	image			[img]...[/img]

			//The following tags are not parsed, and don't need to be parsed:
			//	quote			[quote]...[/quote]


			//string parsedMessage = System.Text.RegularExpressions.Regex.Replace(message, @"\[B\]([\s\S].*)\[/B\]", "<b>${inner}</b>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);

			while (Regex.IsMatch(message, @"\[quote]([\s\S]*?)\[/quote\]\r\n", RegexOptions.IgnoreCase))
				message = Regex.Replace(message, @"\[quote](?<inner>([\s\S]*?))\[/quote\]\r\n", "<div class=\"quoteHeader\"><small>quote:</small></div><div class=\"quoteContent\">${inner}</div>", RegexOptions.IgnoreCase);

			while (Regex.IsMatch(message, @"\[quote]([\s\S]*?)\r\n\[/quote\]", RegexOptions.IgnoreCase))
				message = Regex.Replace(message, @"\[quote](?<inner>([\s\S]*?))\r\n\[/quote\]", "<div class=\"quoteHeader\"><small>quote:</small></div><div class=\"quoteContent\">${inner}</div>", RegexOptions.IgnoreCase);



			string parsedMessage = System.Text.RegularExpressions.Regex.Replace(message, @"\[B\](?<inner>([\s\S]*?))\[/B\]", "<b>${inner}</b>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
			parsedMessage = Regex.Replace(parsedMessage, @"\[I\](?<inner>([\s\S]*?))\[/I\]", "<i>${inner}</i>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
			parsedMessage = System.Text.RegularExpressions.Regex.Replace(parsedMessage, @"\[U\](?<inner>([\s\S]*?))\[/U\]", "<u>${inner}</u>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
			parsedMessage = System.Text.RegularExpressions.Regex.Replace(parsedMessage, @"\[url\=(?<http>(skype:)|(http://)|(https://)|(ftp://)|(ftps://))?(?<url>([^\]]*?))\](?<inner>(.*?))\[/url\]", "<a href=\"${http}${url}\">${inner}</a>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);

			parsedMessage = System.Text.RegularExpressions.Regex.Replace(parsedMessage, @"\[img](?<inner>(.*?))\[/img]", "<img src=\"${inner}\"</img>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);


			//YOUTUBE
			while (Regex.IsMatch(parsedMessage, @"\[youtube](?<inner>(.*?))\[/youtube]", RegexOptions.IgnoreCase))
			{
				string ytUrl = Regex.Match(parsedMessage, @"\[youtube](?<inner>(.*?))\[/youtube]").Groups["inner"].ToString();
				string moviecode = ytUrl.Substring(31, 11);
				string embeddedUrl = "http://www.youtube.com/v/" + moviecode;

				string match = "[youtube]" + ytUrl + "[/youtube]";

				parsedMessage = parsedMessage.Replace(match, "<object width=\"560\" height=\"340\"><param name=\"movie\" value=\"" + embeddedUrl + "\"></param><param name=\"allowFullScreen\" value=\"true\"></param><param name=\"allowscriptaccess\" value=\"always\"></param><embed src=\"" + embeddedUrl + "\" type=\"application/x-shockwave-flash\" allowscriptaccess=\"always\" allowfullscreen=\"true\" width=\"560\" height=\"340\"></embed></object>");
			}



			//YOUTUBE
			while (Regex.IsMatch(parsedMessage, @"\[yt](?<inner>(.*?))\[/yt]", RegexOptions.IgnoreCase))
			{
				string ytUrl = Regex.Match(parsedMessage, @"\[yt](?<inner>(.*?))\[/yt]").Groups["inner"].ToString();
				string moviecode = ytUrl.Substring(31, 11);
				string embeddedUrl = "http://www.youtube.com/v/" + moviecode;

				string match = "[yt]" + ytUrl + "[/yt]";

				parsedMessage = parsedMessage.Replace(match, "<object width=\"560\" height=\"340\"><param name=\"movie\" value=\"" + embeddedUrl + "\"></param><param name=\"allowFullScreen\" value=\"true\"></param><param name=\"allowscriptaccess\" value=\"always\"></param><embed src=\"" + embeddedUrl + "\" type=\"application/x-shockwave-flash\" allowscriptaccess=\"always\" allowfullscreen=\"true\" width=\"560\" height=\"340\"></embed></object>");
				//parsedMessage = System.Text.RegularExpressions.Regex.Replace(parsedMessage, match, "<object width=\"560\" height=\"340\"><param name=\"movie\" value=\"" + embeddedUrl + "\"></param><param name=\"allowFullScreen\" value=\"true\"></param><param name=\"allowscriptaccess\" value=\"always\"></param><embed src=\"" + embeddedUrl + "\" type=\"application/x-shockwave-flash\" allowscriptaccess=\"always\" allowfullscreen=\"true\" width=\"560\" height=\"340\"></embed></object>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
			}

			parsedMessage = parsedMessage.Replace("<a href=\"www", "<a href=\"http://www");
			parsedMessage = parsedMessage.Replace("\r\n", "<br/>");
			parsedMessage = parsedMessage.Replace("\n", "<br/>");
			return parsedMessage;
		}


		/// <summary>
		/// Forum users are not allowed to add html-tags to their messages.
		/// This method deletes all the html-tags.
		/// </summary>
		/// <param name="message"></param>
		/// <returns></returns>
		protected string RemoveHTML(string message)
		{
			//All tags are case insensitive. So <b>...</b> is parsed, as well as <B>...</B>

			//The following tages are deleted:
			//	bold Start				<b>
			//	bold End				</b>
			//	italic Start			<i>
			//	italic End				</i>
			//	underline Start			<u>
			//	underline End			</u>
			//	strikethrough Start		<s>
			//	strikethrough End		</s>

			//The following tags are not yet parsed, but have to be parsed:
			//	hyperlink Start			<a href>
			//	hyperlink End			</a href>
			//	hyperlink End			</a>
			//	image					<img>
			//	image End				</img>

			//The following tags are not parsed, and don't need to be parsed:
			//	quote Start				<quote>
			//	quote End				</quote>
			//	hyperlink Start			<url=...>
			//	hyperlink End			</url>



			string parsedMessage = System.Text.RegularExpressions.Regex.Replace(message, @"\<B\>", "", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
			parsedMessage = System.Text.RegularExpressions.Regex.Replace(parsedMessage, @"\</B\>", "", System.Text.RegularExpressions.RegexOptions.IgnoreCase);

			parsedMessage = System.Text.RegularExpressions.Regex.Replace(parsedMessage, @"\<I\>", "", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
			parsedMessage = System.Text.RegularExpressions.Regex.Replace(parsedMessage, @"\</I\>", "", System.Text.RegularExpressions.RegexOptions.IgnoreCase);

			parsedMessage = System.Text.RegularExpressions.Regex.Replace(parsedMessage, @"\<U\>", "", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
			parsedMessage = System.Text.RegularExpressions.Regex.Replace(parsedMessage, @"\</U\>", "", System.Text.RegularExpressions.RegexOptions.IgnoreCase);

			parsedMessage = System.Text.RegularExpressions.Regex.Replace(parsedMessage, @"\<S\>", "", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
			parsedMessage = System.Text.RegularExpressions.Regex.Replace(parsedMessage, @"\</S\>", "", System.Text.RegularExpressions.RegexOptions.IgnoreCase);

			parsedMessage = System.Text.RegularExpressions.Regex.Replace(parsedMessage, @"\<URL\>", "", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
			parsedMessage = System.Text.RegularExpressions.Regex.Replace(parsedMessage, @"\</URL\>", "", System.Text.RegularExpressions.RegexOptions.IgnoreCase);

			parsedMessage = System.Text.RegularExpressions.Regex.Replace(parsedMessage, @"\<URL\=", "", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
			parsedMessage = System.Text.RegularExpressions.Regex.Replace(parsedMessage, @"\</URL\>", "", System.Text.RegularExpressions.RegexOptions.IgnoreCase);

			return parsedMessage;
		}


	}
}
